package com.epam.facebook_client;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;

import android.app.Application;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.epam.facebook_client.api.API;
import com.epam.facebook_client.manager.CacheManager;
import com.epam.facebook_client.manager.HttpClientManager;
import com.epam.facebook_client.manager.HttpClientManager.AbstractRule;
import com.epam.facebook_client.manager.ImageManager;
import com.epam.facebook_client.processor.AlbumsProcessor;
import com.epam.facebook_client.processor.ProcessorFactory;
import com.epam.facebook_client.processor.FeedsProcessor;
import com.epam.facebook_client.processor.FriendsProcessor;
import com.epam.facebook_client.processor.PhotosProcessor;
import com.epam.facebook_client.services.ServiceHelper;

public class CoreApplication extends Application {
	private ImageManager imageManager;
	private HttpClientManager httpClientManager;
	private ServiceHelper serviceHelper;
	private CacheManager cacheManager;

	@Override
	public void onCreate() {
		super.onCreate();
		httpClientManager = new HttpClientManager();
		imageManager = new ImageManager(this);
		serviceHelper = new ServiceHelper(getApplicationContext());
		cacheManager = new CacheManager(getApplicationContext());
		
		httpClientManager.addRule(new AbstractRule() {

			public boolean isMatch(String url) {
				return url.startsWith(API.BASE_PATH);
			}

			public HttpUriRequest execute(String url) {
				String token = getSharedPreferences(StartActivity.FILE_NAME, 0).getString(
						StartActivity.TOKEN, null);
				if (token == null || token.equals("")) {
					startActivity(new Intent(getApplicationContext(), StartActivity.class));
				}
				Uri uri = Uri.parse(url);
				String requestType = uri.getQueryParameter("request_type");
				String needOauth = uri.getQueryParameter("need_oauth");
				if (TextUtils.isEmpty(requestType)) {
					requestType = "get";
				}
				if (TextUtils.isEmpty(needOauth)) {
					needOauth = "true";
				}

				if (requestType.equals("get") && needOauth.equals("true")) {
					url = url + "&access_token=" + token;
					return new HttpGet(url);
				}
				if (requestType.equals("get") && needOauth.equals("false")) {
					return new HttpGet(url);
				}
				if (requestType.equals("post") && needOauth.equals("true")) {
					url = url + "&access_token=" + token;
					return new HttpPost(url);
				}
				return new HttpGet(url);
			}
		});
		ProcessorFactory factoryProcessor = ProcessorFactory.getInstance();
		factoryProcessor.addProcessor(AlbumsProcessor.ACTION, new AlbumsProcessor(getApplicationContext()));
		factoryProcessor.addProcessor(FeedsProcessor.ACTION, new FeedsProcessor(getApplicationContext()));
		factoryProcessor.addProcessor(FriendsProcessor.ACTION, new FriendsProcessor(getApplicationContext()));
		factoryProcessor.addProcessor(PhotosProcessor.ACTION, new PhotosProcessor(getApplicationContext()));
	}

	@Override
	public Object getSystemService(String name) {
		if (name.equals(HttpClientManager.SERVICE_NAME)) {
			return httpClientManager;
		}
		if (name.equals(ImageManager.SERVICE_NAME)) {
			return imageManager;
		}
		if (name.equals(ServiceHelper.SERVICE_NAME)) {
			return serviceHelper;
		}
		if (name.equals(CacheManager.SERVICE_NAME)) {
			return cacheManager;
		}
		return super.getSystemService(name);
	}
}
